package com.hudongwx.food.login_fargment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.hudongwx.food.MyApplication;
import com.hudongwx.food.R;
import com.hudongwx.food.utils.API;
import com.hudongwx.food.utils.MyJsonObject;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.x;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * A simple {@link Fragment} subclass.
 */
@ContentView(R.layout.fragment_shouji)
public class ShoujiFragment extends Fragment {
    private View view;
    private EditText et1, et2;
    private Button btn;
    private JSONObject jojb;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (null == view) {
            view = x.view().inject(this, inflater, container);
            et1 = (EditText) view.findViewById(R.id.shouji_acc);
            et2 = (EditText) view.findViewById(R.id.shouji_pwd);
            btn = (Button) view.findViewById(R.id.shouji_button);


            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!(TextUtils.isEmpty(et1.getText())&&TextUtils.isEmpty(et2.getText()))){
                        RequestParams params = new RequestParams(API.loginUrl);
                        params.addBodyParameter("Action","usePwd");
                        params.addBodyParameter("Md5Pwd",md5(et2.getText().toString()).toUpperCase());
                        params.addBodyParameter("phone",et1.getText().toString());
                        x.http().post(params, new Callback.CommonCallback<MyJsonObject>() {
                            @Override
                            public void onSuccess(MyJsonObject result) {
                                try {
                                    MyApplication.Uid=result.getString("Status");
//                                    String in = MyApplication.Uid;
                                    jojb = result.getJSONObject("Data");
                                    MyApplication.storage(MyApplication.Uid);
                                    Toast.makeText(getActivity(),"登陆成功",Toast.LENGTH_SHORT).show();

                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }

                            @Override
                            public void onError(Throwable ex, boolean isOnCallback) {

                            }

                            @Override
                            public void onCancelled(CancelledException cex) {

                            }

                            @Override
                            public void onFinished() {

                            }
                        });

                    }else {
                        Toast.makeText(getActivity(),"账号或密码不能为空",Toast.LENGTH_SHORT).show();
                    }
                }
            });

//
        }


        return view;
    }
public String md5(String string){
   if (TextUtils.isEmpty(string)){
       return "";
   }
    MessageDigest md5 = null;
    try {
        md5 = MessageDigest.getInstance("MD5");
        byte [] bytes = md5.digest(string.getBytes());
        String result = "";
        for (byte  b: bytes) {
           String temp = Integer.toHexString(b & 0xff);
            if (temp.length()==1){
                temp = "0" +temp;
            }
            result += temp;
        }
        return result;
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return "";
}

}
